$(document).ready(function () {
  var url = 'http://127.0.0.1:8199/';

  function init() {
    $.ajax({
      type: 'get',
      url: url + 'jpa/getUserList.do',
      dataType: "json",
      success: function (map) {
        var html = '<tr><th>序号</th><th>账号</th><th>角色</th><th>姓名</th><th>邮箱</th><th>操作</th></tr>';
        if (map.success) {
          $.each(map.data, function (index, d) {
            html += '<tr class="active">'
              + '<td>' + (index + 1) + '</td>'
              + '<td>' + d.user_account + '</td>'
              + '<td>' + d.user_role + '</td>'
              + '<td>' + d.user_name + '</td>'
              + '<td>' + d.user_email + '</td>'
              + '<td> <a  title="编辑" id="edit" class="btn btn-success btn-sm " data-toggle="modal" data-target="#myModal">\n' +
              '          <span class="glyphicon glyphicon-pencil"></span>\n' +
              '        </a>' + '&nbsp;' +
              '<button  title="删除" id="del"  class="btn btn-danger btn-sm">' +
              '                        <span class="glyphicon glyphicon-trash"></span>\n' +
              '                        </button></td>'
              + '</tr>'
          });
          $('#tb').html(html);
          //document.getElementById('tb').innerHTML=html;
        } else {
          layer.alert('发生异常');
        }
      }
    })
  }

  init();
// 删除操作
  $("#del").click(function () {
    $.ajax({
      type: "post",
      url: url + "/admin/deleteUser.do",
      dataType: "json",
      data: {user_id: 2},
      success: function (d) {
        if (d.success) {
          layer.alert('删除成功');
        } else {
          layer.alert('删除失败');
        }
      }
    })
  });
  //提交按钮新增用户
  $("#save").click(function () {
    var account = $('#account').val();
    var userpwd = $('#userpwd').val();
    var radio = $("input[name='optionsRadios']:checked").val();
    var username = $('#username').val();
    var inputEmail3 = $('#inputEmail3').val();
    var user_college = $('#user_college').val();
    var cur = $("#cur").val();
    $.ajax({
      type: "post",
      url: url + "/admin/addUser.do",
      dataType: "json",
      data: {
        user_account: account,
        user_pwd: userpwd,
        user_role: radio,
        user_name: username,
        user_email: inputEmail3,
        user_college: user_college,
        course_id: cur
      },
      success: function (v) {
        if (v.success){
          layer.alert("添加成功!");
          $('#myModal').fadeOut();
        } else {
          layer.alert("添加失败!");
        }
      }
    })
  })
  
  $("#edit").click(function () {

  })
});
